#include "building.h"

Building::Building(QPoint enterence, QPixmap buildingPixmap, QGraphicsScene *scene)
    : QGraphicsPixmapItem(buildingPixmap)
{
    scene->addItem(this);

    setAcceptHoverEvents(true);

    setData(ITEM_TYPE,GameItems::Building);

    overlayPixmap = buildingPixmap;
    overlayPixmap.fill(QColor(255,0,0,180));
    overlayPixmap.setAlphaChannel(pixmap());

    toolTip = new BuildingToolTip(scene);
    infectedPeople = 0;
    buildingHealth = 0;

    enterencePoint = enterence;
    sterility = 95;
    setZValue(2);

    isPlayingToolAnimation = false;
}

void Building::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    emit clicked(this);
}

void Building::addPerson(Person *newPerson)
{
    currentPeople.append(newPerson);
}

void Building::removePerson(Person *oldPerson)
{
    currentPeople.removeAll(oldPerson);
}

void Building::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    //QGraphicsPixmapItem::paint(painter,option,widget);

    // If the building is infected draw the red overlay
    if (((qreal)infectedPeople/currentPeople.length()) > 0.5)
    {
        painter->drawPixmap(0,0,overlayPixmap);
    }

    if (isPlayingToolAnimation)
    {
        painter->setPen(QPen(QBrush(QColor(255, 255, 255, (255 * ((50 - toolCircleRadius) / 50.0)))), 10));
        painter->drawEllipse(this->boundingRect().center(), toolCircleRadius, toolCircleRadius);
    }
}

void Building::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    toolTip->moveToolTip(event->scenePos());
    toolTip->show();
    emit hoverChanged(true);
}

void Building::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    toolTip->hide();
    emit hoverChanged(false);
}

void Building::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
    toolTip->moveToolTip(event->scenePos());
}

void Building::updateToolTipData()
{
    int newInfected = 0;

    for (int index = 0; index < currentPeople.length(); index++)
    {
        if (currentPeople.at(index)->virus != 0)
            newInfected++;
    }

    if (currentPeople.length()>0)   // We have to do this so we don't get a divide by zero error with no people in the house
        buildingHealth = (((double)(currentPeople.length()-newInfected)/currentPeople.length()));
    else
        buildingHealth = 1;

    if (infectedPeople != newInfected)
    {
        infectedPeople = newInfected;
        scene()->update(mapToScene(boundingRect()).boundingRect());
    }

    toolTip->updateToolTip(buildingHealth, currentPeople.length()*10, infectedPeople*10);
}

void Building::playToolAnimation()
{
    isPlayingToolAnimation = true;
    toolCircleRadius = 1;
}

void Building::advance(int phase)
{

    if ((phase == 0))
    {
        for(int currentPerson = 0; currentPerson < currentPeople.length(); currentPerson++)
        {
            for(int otherPerson = currentPerson+1; otherPerson < currentPeople.length(); otherPerson++)
            {
                if((rand() % 100) >= sterility &&
                   currentPeople.at(currentPerson)->GetInBuilding() && currentPeople.at(otherPerson)->GetInBuilding())
                {
                    currentPeople.at(currentPerson)->collidesWithPerson(currentPeople.at(otherPerson));
                }
            }
        }

        if (isPlayingToolAnimation)
        {
            toolCircleRadius += 1;
            if (toolCircleRadius > 50)
                isPlayingToolAnimation = false;
        }
    }
}

QList<Person*> Building::CurrentPeopleAccessor()
{
    return currentPeople;
}

QPoint Building::getEnterencePoint()
{
    return enterencePoint;
}

bool Building::isInfected()
{
    return (((qreal)infectedPeople/currentPeople.length()) > 0.5);
}

int Building::SterilityGetter()
{
    return sterility;
}

void Building::SterilitySetter(int value)
{
    sterility = value;
    sterility = Utils::clamp(sterility, 0, 100);
}
